package com.qlslylq.ad.sdk.config;

import android.content.Context;
import android.text.TextUtils;

import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.qlslylq.ad.sdk.application.AdMergeSDK;
import com.qlslylq.ad.sdk.application.AdMergeSDKApp;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;

import static com.qlslylq.ad.sdk.model.rsp.MediaWebVO.NOT_CONFIG;


/**
 * %s初始化控制类<br/>
 */
public class CsjMgrHolder {

    private static boolean initSuccess = false;

    public static TTAdManager get() {
        return TTAdSdk.getAdManager();
    }

    public static TTAdNative createAdNative(Context context) {
        return get().createAdNative(context);
    }

    public static void init(final Context context, final InitCallback callback) {
        doInit(context, callback);
    }

    /**
     * 异步初始化(同步初始化函数已废弃)<br/>
     */
    private static void doInit(Context context, final InitCallback callback) {
        AdPlatformEnum platform = AdPlatformEnum.PLATFORM_CSJ;
        if (!initSuccess) {
            try {
                String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
                if (NOT_CONFIG.equals(appId)) {
                    Log.d(String.format("未配置%s账号到蒂烨平台", platform.getId()));
                    return;
                }
                if (TextUtils.isEmpty(appId)) {
                    Log.d(String.format("%s初始化失败：" + GsonUtils.gsonString(ErrorEnum.ERR_INIT_INVALID_ARG.rsp(platform, null)), platform.getId()));
                    if (callback != null) {
                        callback.fail(platform, ErrorEnum.ERR_INIT_INVALID_ARG.getId(), ErrorEnum.ERR_INIT_INVALID_ARG.getName());
                    }
                    return;
                }

                TTAdSdk.init(context, buildConfig(platform), new TTAdSdk.InitCallback() {
                    public void success() {
                        initSuccess = TTAdSdk.isInitSuccess();
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败"), platform.getId()));
                    }

                    public void fail(int code, String msg) {
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + code + "：" + msg, platform.getId()));
                        if (callback != null) {
                            callback.fail(platform, code, msg);
                        }
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
                Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + e.getMessage(), platform.getId()));
                if (callback != null) {
                    callback.fail(platform, ErrorEnum.ERR_INIT_THIRD_SDK.getId(), e.getMessage());
                }
            }
        }
    }

    private static TTAdConfig buildConfig(AdPlatformEnum platform) {
        String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
        return new TTAdConfig.Builder()
                .appId(appId)
                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                .allowShowNotify(true) //是否允许sdk展示通知栏提示,若设置为false则会导致通知栏不显示下载进度
                .debug(AdMergeSDKApp.isDebug()) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
                .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_3G) //允许直接下载的网络状态集合
                .supportMultiProcess(false)//是否支持多进程
                .needClearTaskReset()
                .build();
    }

    public static boolean isInitSuccess() {
        return initSuccess;
    }

    /**
     * 设置是否启用平台<br/>
     */
    public static void enablePlatform(boolean enable) {
        initSuccess = enable;
    }
}
